package com.bicon.life.core.base.repository.jpa;

import javax.persistence.criteria.*;

/**
 * Created by zhoubin on 2016/1/8.
 */
public class SimpleExpression<T> implements Criterion<T>{

    private String fieldName;       //属性名
    private Object value;           //值
    private Object value1;          //值1
    private Operator operator;      //操作类型
    private Class c;                //关联类
    private Boolean isJoin;         //是否关联查询

    protected SimpleExpression(String fieldName, Object value, Operator operator) {
        this.fieldName = fieldName;
        this.value = value;
        this.operator = operator;
    }

    protected SimpleExpression(String fieldName, Object value, Object value1, Operator operator) {
        this.fieldName = fieldName;
        this.value = value;
        this.value1 = value1;
        this.operator = operator;
    }

    protected SimpleExpression(String fieldName, Object value, Class c, Operator operator, Boolean isJoin) {
        this.fieldName = fieldName;
        this.value = value;
        this.c = c;
        this.operator = operator;
        this.isJoin = isJoin;
    }

    public String getFieldName() {
        return fieldName;
    }
    public Object getValue() {
        return value;
    }
    public Operator getOperator() {
        return operator;
    }

    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
                                 CriteriaBuilder builder) {
        Path expression = null;
        if(Boolean.TRUE == isJoin){
            if(fieldName.contains(".")) {
                String[] names = fieldName.split("\\.");
                ListJoin join = root.join(root.getModel().getList(names[0], c), JoinType.LEFT);
                expression = join.get(names[1]);
            }
        }else{
            if(fieldName.contains(".")){
                String[] names = fieldName.split("\\.");
                expression = root.get(names[0]);
                for (int i = 1; i < names.length; i++) {
                    expression = expression.get(names[i]);
                }
            }else{
                expression = root.get(fieldName);
            }
        }

        switch (operator) {
            case NU:
                return builder.isNull(expression);
            case NNU:
                return builder.isNotNull(expression);
            case EQ:
                return builder.equal(expression, value);
            case NE:
                return builder.notEqual(expression, value);
            case LIKE:
                return builder.like((Expression<String>) expression, "%" + value + "%");
            case NOTLIKE:
                return builder.notLike((Expression<String>) expression, "%" + value + "%");
            case LT:
                return builder.lessThan(expression, (Comparable) value);
            case GT:
                return builder.greaterThan(expression, (Comparable) value);
            case LTE:
                return builder.lessThanOrEqualTo(expression, (Comparable) value);
            case GTE:
                return builder.greaterThanOrEqualTo(expression, (Comparable) value);
            case BETWEEN:
                return builder.between(expression, (Comparable) value, (Comparable) value1);
            default:
                return null;
        }
    }

}